package com.zggk.lzz.roadh;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Switch;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.zggk.lzz.R;
import com.zggk.lzz.api.MyApplication;
import com.zggk.lzz.dtfd.DtfdActivity;
import com.zggk.lzz.dw.AMapUtils;
import com.zggk.lzz.dw.LocationService;
import com.zggk.lzz.dw.LocationStatusManager;
import com.zggk.lzz.land.LandActivity;
import com.zggk.lzz.mvp.MVPBaseActivity;
import com.zggk.lzz.sjcx.SjcxActivity;
import com.zggk.lzz.sjsb.SjsbActivity;
import com.zggk.lzz.updata.UpdateManager;
import com.zggk.lzz.util.CommBtnListener;
import com.zggk.lzz.util.CommNotificationDialog;
import com.zggk.lzz.util.Utils;
import com.zggk.lzz.xcbgadd.XcbgAddActivity;
import com.zggk.lzz.xcjl.XcjlActivity;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class RoadHActivity extends MVPBaseActivity<RoadHContract.View, RoadHPresenter> implements RoadHContract.View {

    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.cancle_button)
    TextView cancleButton;
    @Bind(R.id.header_lay)
    RelativeLayout headerLay;
    @Bind(R.id.name_img)
    ImageView nameImg;
    @Bind(R.id.name_te)
    TextView nameTe;
    @Bind(R.id.lz_img)
    ImageView lzImg;
    @Bind(R.id.name_lay)
    LinearLayout nameLay;
    @Bind(R.id.xj_te)
    TextView xjTe;
    @Bind(R.id.ksxc_re)
    ImageView ksxcRe;
    @Bind(R.id.sjsb_im)
    ImageView sjsbIm;
    @Bind(R.id.xcjl_im)
    ImageView xcjlIm;
    @Bind(R.id.sjcx_im)
    ImageView sjcxIm;
    @Bind(R.id.dtfb_im)
    ImageView dtfbIm;
    @Bind(R.id.activity_disease_new_scrollview)
    ScrollView activityDiseaseNewScrollview;
    @Bind(R.id.disease_new_parent_layout)
    RelativeLayout diseaseNewParentLayout;
    @Bind(R.id.wzxx_te)
    TextView wzxxTe;
    @Bind(R.id.jrgz_te)
    TextView jrgzTe;
    @Bind(R.id.st_switch)
    Switch stSwitch;
    private String isXC = "1";
    public static final String RECEIVER_ACTION = "location_in_background";
    private CommNotificationDialog Dialogs;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initView();
//        initData();
        new UpdateManager(this).checkUpdate();   //检查更新
    }

    @Override
    protected int getLayout() {
        return R.layout.act_road_h;
    }

    @Override
    public void onRequestError(String msg) {

    }

    @OnClick({R.id.ksxc_re, R.id.sjsb_im, R.id.sjcx_im, R.id.xcjl_im, R.id.cancle_button, R.id.dtfb_im})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancle_button:
                MyApplication.spUtils.clear();
                Intent intent5 = new Intent(RoadHActivity.this, LandActivity.class);
                startActivity(intent5);
                finish();
                break;
            case R.id.ksxc_re:
                if (!Utils.isLocationEnabled()) {
                    ToastUtils.showShort("请打开定位功能");
                    return;
                }
                if (isXC.equals("1")) {
                    if (Dialogs == null) {
                        Dialogs = new CommNotificationDialog(getContext(), "", "请选择巡查模式", "步行巡查", "车辆巡查", new CommBtnListener() {
                            @Override
                            public void CommOkBtnClick() {
                                MyApplication.spUtils.put("XCMS", 10);
                                Glide.with(RoadHActivity.this)
                                        .load(R.drawable.jsxc_img)
                                        .into(ksxcRe);
                                isXC = "2";
                                startLocationService();
                                jrgzTe.setText("巡查进行中...");
                                jrgzTe.setTextColor(getResources().getColor(R.color.xc_color));
                                Dialogs.close();
                            }

                            @Override
                            public void CommCancelBtnClick() {
                                MyApplication.spUtils.put("XCMS", 3);
                                Glide.with(RoadHActivity.this)
                                        .load(R.drawable.jsxc_img)
                                        .into(ksxcRe);
                                isXC = "2";
                                startLocationService();
                                jrgzTe.setText("巡查进行中...");
                                jrgzTe.setTextColor(getResources().getColor(R.color.xc_color));
                                Dialogs.close();
                            }
                        });
                    }
                    Dialogs.show();
                } else {
                    Glide.with(RoadHActivity.this)
                            .load(R.drawable.ksxc_img)
                            .into(ksxcRe);
                    isXC = "1";
                    stopLocationService();
                    Intent intent = new Intent(RoadHActivity.this, XcbgAddActivity.class);
                    startActivity(intent);
                    jrgzTe.setText("今日工作");
                    jrgzTe.setTextColor(getResources().getColor(R.color.text_color));
                }
                LocationStatusManager.getInstance().resetToInit(getApplicationContext());
                break;
            case R.id.sjsb_im:
                Intent intent = new Intent(RoadHActivity.this, SjsbActivity.class);
                startActivity(intent);
                break;
            case R.id.xcjl_im:
                Intent intent1 = new Intent(RoadHActivity.this, XcjlActivity.class);
                startActivity(intent1);
                break;
            case R.id.sjcx_im:
                Intent intent2 = new Intent(RoadHActivity.this, SjcxActivity.class);
                startActivity(intent2);
                break;
            case R.id.dtfb_im:
                Intent intent3 = new Intent(RoadHActivity.this, DtfdActivity.class);
                startActivity(intent3);
                break;
        }
    }

    private void initView() {
        MyApplication.spUtils.put("XCMS", 10);
        xjTe.setText(MyApplication.spUtils.getString("OrgName"));
        wzxxTe.setText(MyApplication.spUtils.getString("DemonstrationUnit"));
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(RECEIVER_ACTION);
        registerReceiver(locationChangeBroadcastReceiver, intentFilter);
        nameTe.setText(MyApplication.spUtils.getString("UserName"));
        stSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    stSwitch.setChecked(true);
                } else {
                    stSwitch.setChecked(false);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (locationChangeBroadcastReceiver != null)
            unregisterReceiver(locationChangeBroadcastReceiver);
        super.onDestroy();
    }

    /**
     * 开始定位服务
     */
    private void startLocationService() {
        getApplicationContext().startService(new Intent(RoadHActivity.this, LocationService.class));
        RoadSetBean roadSetBean = new RoadSetBean();
        roadSetBean.setUserId(MyApplication.spUtils.getString("UserID"));
        roadSetBean.setLon("");//经度116.2
        roadSetBean.setLat("");//纬度39.56
        roadSetBean.setNowTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        String sss = JSON.toJSONString(roadSetBean);
        mPresenter.setRoad(sss);
    }

    /**
     * 关闭服务
     * 先关闭守护进程，再关闭定位服务
     */
    private void stopLocationService() {
        sendBroadcast(AMapUtils.getCloseBrodecastIntent());
    }

    private BroadcastReceiver locationChangeBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(RECEIVER_ACTION)) {
                if (stSwitch.isChecked()){
                    ToastUtils.showShort("巡查正常:"+intent.getStringExtra("Lon")+","+intent.getStringExtra("Lat"));
                }else {

                }
                RoadSetBean roadSetBean = new RoadSetBean();
                roadSetBean.setUserId(MyApplication.spUtils.getString("UserID"));
                roadSetBean.setLon(intent.getStringExtra("Lon"));//经度116.2
                roadSetBean.setLat(intent.getStringExtra("Lat"));//纬度39.56
                roadSetBean.setNowTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                String sss = JSON.toJSONString(roadSetBean);
                mPresenter.setRoad2(sss);
            }
        }
    };
}
